
## 来自gitlab的源路径
WEBRTC_ORIGIN = https://gitlab.com/webrtc-mirror

## make 时不传参 PLAT 时使用下面配置
PLAT ?= x64

ROOT = $(shell pwd)
INSTALL_PATH = $(ROOT)/install_$(PLAT)
LIBS_PATH = $(ROOT)/libs
WEBRTC_SRC = $(ROOT)/src

include $(ROOT)/platform/$(PLAT).mk
include $(ROOT)/sourcelist.mk

## 找不到 plat.mk 文件时的默认配置
PLAT_CPU ?= x64
GCC ?= gcc
GXX ?= g++
AR ?= ar


##### ----- 公共编译配置 ----- #####

LIB_HEAD = libwebrtc_
LIB_HEAD_BUILD = -Wl,-soname,$(LIB_HEAD)
LIB_TAIL = so

INCS += $(EXT_INCS)
INCS += -I$(WEBRTC_SRC)
INCS += -I$(WEBRTC_SRC)/third_party/abseil-cpp

LIBS += $(EXT_LIBS)

CFLAGS += $(EXT_CFLAGS)
CFLAGS += -Wall -fPIC -lm
CFLAGS += -s

##### ----- 平台相关配置 ----- #####

## 系统相关宏: WEBRTC_LINUX WEBRTC_WIN WEBRTC_ANDROID WEBRTC_IOS WEBRTC_MAC
## 系统相关宏: OS_LINUX OS_WIN OS_ANDROID OS_IOS OS_MACOS
## 架构相关宏: __i386__ __x86_64__ __ARMEL__ __aarch64__ __MIPSEL__

## linux系统三件套
DEFINES += -DWEBRTC_LINUX -DOS_LINUX -DWEBRTC_POSIX

ifeq ($(PLAT_CPU),x64)
DEFINES += -D__x86_64__
else ifeq ($(PLAT_CPU),arm)
DEFINES += -D__ARMEL__
else ifeq ($(PLAT_CPU),arm64)
DEFINES += -D__aarch64__
endif

##### ----- webrtc相关配置 ----- #####

DEFINES += -DWEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS=0
DEFINES += -DWEBRTC_APM_DEBUG_DUMP=0
# DEFINES += -DWEBRTC_CHROMIUM_BUILD

##### ----- 编译项汇总 ----- #####

default_target += env
default_target += ns
default_target += legacy_agc
default_target += vad
default_target += aecm
ifneq ($(PLAT), x64-ohos)
ifeq ($(PLAT_CPU), x64)
default_target += agc
default_target += aec3
endif
endif

default_install += env
default_install += ns_install
default_install += legacy_agc_install
default_install += vad_install
default_install += aecm_install
ifneq ($(PLAT), x64-ohos)
ifeq ($(PLAT_CPU), x64)
default_install += agc_install
default_install += aec3_install
endif
endif


all: $(default_target)
	@echo "========== all complete =========="

install: $(default_install)
	@echo "========== install complete =========="

clean:
	@rm -f *.a *.so *.dll $(NS_OBJS) $(AGC_OBJS) $(LEGACY_AGC_OBJS) $(VAD_OBJS) $(AECM_OBJS) $(AEC3_OBJS) && \
	make -C $(ROOT)/test clean

distclean: clean
	@rm -rf $(INSTALL_PATH)


##### ----- webrtc adapt ----- #####

webrtcadapt:
	$(GCC) -shared -o $(INSTALL_PATH)/lib/lib$@.so -Wl,-soname,lib$@.so \
	$(ROOT)/src_webrtcadapt/webrtcadapt.cpp \
	$(INCS) -I$(INSTALL_PATH)/include $(LIBS) -L$(INSTALL_PATH)/lib $(CFLAGS) \
	-l:libwebrtc_legacy_agc.a -l:libwebrtc_vad.a -l:libwebrtc_aecm.a && \
	cp $(ROOT)/src_webrtcadapt/webrtcadapt.h $(INSTALL_PATH)/include/

##### ----- 音频模块独立编译 ----- #####

## webrtc_ns (先使用for循环编译.o 再生成动态库和静态库)
NS_CFLAGS += $(CFLAGS)
ns:
	@mkdir -p $(INSTALL_PATH)/include $(INSTALL_PATH)/lib && \
	for x in $(NS_SRCS); do \
		echo "$(GCC) -c $(WEBRTC_SRC)/$$x -o $(WEBRTC_SRC)/$$x.o $(INCS) $(DEFINES) $(NS_CFLAGS)" ; \
		$(GCC) -c $(WEBRTC_SRC)/$$x -o $(WEBRTC_SRC)/$$x.o $(INCS) $(DEFINES) $(NS_CFLAGS) ; \
	done && \
	$(GCC) -shared -o $(INSTALL_PATH)/lib/$(LIB_HEAD)$@.$(LIB_TAIL) $(NS_OBJS) $(LIB_HEAD_BUILD)$@.$(LIB_TAIL) $(DEFINES) $(NS_CFLAGS) && \
	$(AR) -rcs $(INSTALL_PATH)/lib/$(LIB_HEAD)$@.a $(NS_OBJS) && \
	rm -f $(NS_OBJS)
ns_install:
	@cd $(WEBRTC_SRC) && \
	for x in $(NS_INCS); do cp --parents ./$$x $(INSTALL_PATH)/include ; done && \
	cd -
ns_srcs_show:
	@echo $(NS_SRCS)
ns_flags_show:
	@echo $(DEFINES) $(NS_CFLAGS) $(INCS)

## webrtc_vad
VAD_CFLAGS += $(CFLAGS)
vad:
	@mkdir -p $(INSTALL_PATH)/include $(INSTALL_PATH)/lib && \
	for x in $(VAD_SRCS); do \
		echo "$(GCC) -c $(WEBRTC_SRC)/$$x -o $(WEBRTC_SRC)/$$x.o $(INCS) $(DEFINES) $(VAD_CFLAGS) " ; \
		$(GCC) -c $(WEBRTC_SRC)/$$x -o $(WEBRTC_SRC)/$$x.o $(INCS) $(DEFINES) $(VAD_CFLAGS) ; \
	done && \
	$(GCC) -shared -o $(INSTALL_PATH)/lib/$(LIB_HEAD)$@.$(LIB_TAIL) $(VAD_OBJS) $(LIB_HEAD_BUILD)$@.$(LIB_TAIL) $(DEFINES) $(VAD_CFLAGS) && \
	$(AR) -rcs $(INSTALL_PATH)/lib/$(LIB_HEAD)$@.a $(VAD_OBJS) && \
	rm -f $(VAD_OBJS)
vad_install:
	@cd $(WEBRTC_SRC) && \
	for x in $(VAD_INCS); do cp --parents ./$$x $(INSTALL_PATH)/include ; done && \
	cd -
vad_srcs_show:
	@echo $(VAD_SRCS)
vad_flags_show:
	@echo $(DEFINES) $(VAD_CFLAGS) $(INCS)

## webrtc_agc
AGC_CFLAGS += $(CFLAGS) -std=c++17
agc:
	@mkdir -p $(INSTALL_PATH)/include $(INSTALL_PATH)/lib && \
	for x in $(AGC_SRCS); do \
		echo "$(GCC) -c $(WEBRTC_SRC)/$$x -o $(WEBRTC_SRC)/$$x.o $(INCS) $(DEFINES) $(AGC_CFLAGS)" ; \
		$(GCC) -c $(WEBRTC_SRC)/$$x -o $(WEBRTC_SRC)/$$x.o $(INCS) $(DEFINES) $(AGC_CFLAGS) ; \
	done && \
	$(GCC) -shared -o $(INSTALL_PATH)/lib/$(LIB_HEAD)$@.$(LIB_TAIL) $(AGC_OBJS) $(LIB_HEAD_BUILD)$@.$(LIB_TAIL) $(DEFINES) $(AGC_CFLAGS) && \
	$(AR) -rcs $(INSTALL_PATH)/lib/$(LIB_HEAD)$@.a $(AGC_OBJS) && \
	rm -f $(AGC_OBJS)
agc_install:
	@cd $(WEBRTC_SRC) && \
	for x in $(AGC_INCS); do cp --parents ./$$x $(INSTALL_PATH)/include ; done && \
	cd -
agc_srcs_show:
	@echo $(AGC_SRCS)
agc_flags_show:
	@echo $(DEFINES) $(AGC_CFLAGS) $(INCS)

## webrtc_legacy_agc
LEGACY_AGC_CFLAGS += $(CFLAGS)
legacy_agc:
	@mkdir -p $(INSTALL_PATH)/include $(INSTALL_PATH)/lib && \
	for x in $(LEGACY_AGC_SRCS); do \
		echo "$(GCC) -c $(WEBRTC_SRC)/$$x -o $(WEBRTC_SRC)/$$x.o $(INCS) $(DEFINES) $(LEGACY_AGC_CFLAGS)" ; \
		$(GCC) -c $(WEBRTC_SRC)/$$x -o $(WEBRTC_SRC)/$$x.o $(INCS) $(DEFINES) $(LEGACY_AGC_CFLAGS) ; \
	done && \
	$(GCC) -shared -o $(INSTALL_PATH)/lib/$(LIB_HEAD)$@.$(LIB_TAIL) $(LEGACY_AGC_OBJS) $(LIB_HEAD_BUILD)$@.$(LIB_TAIL) $(DEFINES) $(LEGACY_AGC_CFLAGS) && \
	$(AR) -rcs $(INSTALL_PATH)/lib/$(LIB_HEAD)$@.a $(LEGACY_AGC_OBJS) && \
	rm -f $(LEGACY_AGC_OBJS)
legacy_agc_install:
	@cd $(WEBRTC_SRC) && \
	for x in $(LEGACY_AGC_INCS); do cp --parents ./$$x $(INSTALL_PATH)/include ; done && \
	cd -
legacy_agc_srcs_show:
	@echo $(LEGACY_AGC_SRCS)
legacy_agc_flags_show:
	@echo $(DEFINES) $(LEGACY_AGC_CFLAGS) $(INCS)

## webrtc_aecm
AECM_CFLAGS += $(CFLAGS)
aecm:
	@mkdir -p $(INSTALL_PATH)/include $(INSTALL_PATH)/lib && \
	for x in $(AECM_SRCS); do \
		echo "$(GCC) -c $(WEBRTC_SRC)/$$x -o $(WEBRTC_SRC)/$$x.o $(INCS) $(DEFINES) $(AECM_CFLAGS)" ; \
		$(GCC) -c $(WEBRTC_SRC)/$$x -o $(WEBRTC_SRC)/$$x.o $(INCS) $(DEFINES) $(AECM_CFLAGS) ; \
	done && \
	$(GCC) -shared -o $(INSTALL_PATH)/lib/$(LIB_HEAD)$@.$(LIB_TAIL) $(AECM_OBJS) $(LIB_HEAD_BUILD)$@.$(LIB_TAIL) $(DEFINES) $(AECM_CFLAGS) && \
	$(AR) -rcs $(INSTALL_PATH)/lib/$(LIB_HEAD)$@.a $(AECM_OBJS) && \
	rm -f $(AECM_OBJS)
aecm_install:
	@cd $(WEBRTC_SRC) && \
	for x in $(AECM_INCS); do cp --parents ./$$x $(INSTALL_PATH)/include ; done && \
	cd -
aecm_srcs_show:
	@echo $(AECM_SRCS)
aecm_flags_show:
	@echo $(DEFINES) $(AECM_CFLAGS) $(INCS)

## webrtc_aec3
AEC3_CFLAGS += $(CFLAGS) -std=c++17
ifeq ($(PLAT_CPU), x64)
AEC3_CFLAGS += -mavx2 -mfma
endif
aec3:
	@mkdir -p $(INSTALL_PATH)/include $(INSTALL_PATH)/lib && \
	for x in $(AEC3_SRCS); do \
		echo "$(GCC) -c $(WEBRTC_SRC)/$$x -o $(WEBRTC_SRC)/$$x.o $(INCS) $(DEFINES) $(AEC3_CFLAGS)" ; \
		$(GCC) -c $(WEBRTC_SRC)/$$x -o $(WEBRTC_SRC)/$$x.o $(INCS) $(DEFINES) $(AEC3_CFLAGS) ; \
	done && \
	$(GCC) -shared -o $(INSTALL_PATH)/lib/$(LIB_HEAD)$@.$(LIB_TAIL) $(AEC3_OBJS) $(LIB_HEAD_BUILD)$@.$(LIB_TAIL) $(DEFINES) $(AEC3_CFLAGS) && \
	$(AR) -rcs $(INSTALL_PATH)/lib/$(LIB_HEAD)$@.a $(AEC3_OBJS) && \
	rm -f $(AEC3_OBJS)
aec3_install:
	@cd $(WEBRTC_SRC) && \
	for x in $(AEC3_INCS); do cp --parents ./$$x $(INSTALL_PATH)/include ; done && \
	cd -
aec3_srcs_show:
	@echo $(AEC3_SRCS)
aec3_flags_show:
	@echo $(DEFINES) $(AEC3_CFLAGS) $(INCS)

##### ----- 全编译(暂不支持) ----- #####

# rtc_build_ssl=false rtc_ssl_root="$(LIBS_PATH)/include"
build_linux:
	@export PATH=$(PATH):$(ROOT)/depot_tools && \
	cd $(ROOT)/src && \
	gn gen out/Release-clang --args='target_os="linux" target_cpu="x64" is_castos=false is_debug=false is_component_build=false rtc_include_tests=false rtc_build_examples=false' && \
	ninja -C out/Release-clang && \
	cd -

# build_linux:
# 	@export PATH=$(PATH):$(ROOT)/depot_tools && \
# 	cd $(ROOT)/src && \
# 	gn gen out/Release-gcc --args='target_os="linux" target_cpu="x64" is_debug=false is_component_build=false use_sysroot=false is_clang=false use_lld=false treat_warnings_as_errors=false rtc_include_tests=false rtc_build_examples=false use_custom_libcxx=false use_rtti=true' && \
# 	ninja -C out/Release-gcc && \
# 	cd -

# build_linux:
# 	@export PATH=$(PATH):$(ROOT)/depot_tools && \
# 	cd $(ROOT)/src && \
# 	gn gen out/Release --args='is_debug=false target_cpu="x64" rtc_include_tests=false rtc_build_tools=false rtc_build_examples=false' && \
# 	ninja -C out/Release && \
# 	cd -


##### ----- 编译器相关准备 ----- #####

depend:
	@cd $(ROOT)/src && \
	./build/install-build-deps.sh --no-chromeos-fonts && \
	cd -


fit_amd64:
	cd $(ROOT)/src && \
	build/linux/sysroot_scripts/install-sysroot.py --arch=amd64 && \
	cd -

##### ----- 源码环境准备 ----- #####

env: depot_tools src src/base src/build src/buildtools src/testing src/tools src/third_party src/third_party/libyuv src/third_party/catapult src/experiments/registered_field_trials.h

depot_tools:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/depot_tools.git $(ROOT)/$@ && \
	chmod a+x $(ROOT)/depot_tools/cipd
src:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/webrtc.git $(ROOT)/$@
src/base:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/base.git $(ROOT)/$@
src/build:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/build.git $(ROOT)/$@
src/buildtools:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/buildtools.git $(ROOT)/$@
src/testing:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/testing.git $(ROOT)/$@
src/tools:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/tools.git $(ROOT)/$@
src/third_party:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/third_party.git $(ROOT)/$@
src/third_party/libsrtp:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/libsrtp.git $(ROOT)/$@
src/third_party/libyuv:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/libyuv.git $(ROOT)/$@
src/third_party/libjpeg_turbo:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/libjpeg_turbo.git $(ROOT)/$@
src/third_party/nasm:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/nasm.git $(ROOT)/$@
src/third_party/boringssl:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/boringssl.git $(ROOT)/$@
src/third_party/catapult:
	@git clone --depth=1 $(WEBRTC_ORIGIN)/catapult.git $(ROOT)/$@
#	@git clone --depth=1 https://github.com/catapult-project/catapult.git $(ROOT)/$@
src/experiments/registered_field_trials.h:
	@python3 $(ROOT)/src/experiments/field_trials.py header --output $(ROOT)/$@

